using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace KBE_HotFix.Scene
{
    public class SceneManager
    {
        public static SceneManager Ins;
        private Dictionary<string, Action> sceneSetupActions = new Dictionary<string, Action>();
        private Dictionary<string, Action> uiInitActions = new Dictionary<string, Action>();
        public SceneManager()
        {
            if (Ins != null)
                throw new Exception("Only one instance of SceneManager!");
            Ins = this;
        }

        public void OnSceneLoaded(string sceneName)
        {
            Debug.Log("InHotFix: Scene " + sceneName + " loaded");
            Action setupAction;
            if(sceneSetupActions.TryGetValue(sceneName, out setupAction))
            {
                setupAction.Invoke();
            }
        }

        public void RegisterSceneSetup(string name, Action action)
        {
            Action setupAction;
            sceneSetupActions.TryGetValue(name, out setupAction);
            if(setupAction == null)
            {
                sceneSetupActions.Add(name, action);
            }
            else
            {
                setupAction += action;
            }
        }

        public void OnUIRootLoaded(string sceneName)
        {
            Action initAction;
            if(uiInitActions.TryGetValue(sceneName, out initAction))
            {
                initAction.Invoke();
            }
        }

        public void RegisterUIInit(string sceneName, Action action)
        {
            Action initAction;
            uiInitActions.TryGetValue(sceneName, out initAction);
            if(initAction == null)
            {
                uiInitActions.Add(sceneName, action);
            }
            else
            {
                initAction += action;
            }
        }
    }
}